package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.domain.*;
import com.woniu.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.domain.Company;
import com.woniu.domain.User;
import com.woniu.dto.Result;
import com.woniu.mapper.CompanyMapper;
import com.woniu.mapper.UserMapper;
import com.woniu.service.CompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.AllCompanyConditionVo;
import com.woniu.vo.UserAndCompany;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyy
 * @since 2021-03-05
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private PositionMapper positionMapper;
    @Resource
    private UserCompanyMapper userCompanyMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private FeedbackMapper feedbackMapper;
    @Resource
    private CollectMapper collectMapper;

    @Resource
    private UserPositionMapper userPositionMapper;

    @Override
    public Company getCompanyInfo(Integer id) {
        return companyMapper.getCompanyInfo(id);
    }

    @Override
    public IPage<Company> companiesPage(AllCompanyConditionVo allCompanyConditionVo) {
        Page<Company> page = new Page<>(allCompanyConditionVo.getCurrent(),allCompanyConditionVo.getSize());

        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();

        if(!ObjectUtils.isEmpty(allCompanyConditionVo.getName())){
            queryWrapper.like("name",allCompanyConditionVo.getName());
        }

        if(!ObjectUtils.isEmpty(allCompanyConditionVo.getHot())){
            queryWrapper.like("hot",allCompanyConditionVo.getHot());
        }

        IPage<Company> companyIPage = companyMapper.selectPage(page, queryWrapper);

        return companyIPage;
    }

    @Override
    public void deleteCompanyById(Integer cid) {
        //查询相关公司id的职位
        //修改用户的角色为求职者
        QueryWrapper<UserCompany> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("company_id",cid);
        List<UserCompany> userCompanys = userCompanyMapper.selectList(queryWrapper1);

        //删除该公司下面的所有职位以及职位反馈表
        List<Position> positions = positionMapper.lookPositionByCompanyId(cid);
        positions.forEach(position -> {
            positionMapper.deleteById(position);
            //删除职位返回表
            QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("position_id",position.getPositionId());
            feedbackMapper.delete(queryWrapper);
        });

        if(!ObjectUtils.isEmpty(userCompanys)){
            userCompanys.forEach(userCompany -> {
                UserRole entity = new UserRole();
                entity.setRid(3);
                UpdateWrapper<UserRole> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("uid",userCompany.getUserId());
                userRoleMapper.update(entity, updateWrapper);
                //删除这个用户发布的职位中间表
                QueryWrapper<UserPosition> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("uid",userCompany.getUserId());
                userPositionMapper.delete(queryWrapper2);
            });
        }

        companyMapper.deleteById(cid);

        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",cid);
        collectMapper.delete(wrapper);





        QueryWrapper<UserCompany> wrapper4 = new QueryWrapper<>();
        wrapper4.eq("company_id",cid);
        userCompanyMapper.delete(wrapper4);
    }
    @Resource
    private UserMapper userMapper;

    //公司注册
    @Override
    public void addCompany(UserAndCompany userAndCompany) {
        //新增公司信息
        Company company = new Company();
        company.setAddress(userAndCompany.getAddress());
        company.setIndustry(userAndCompany.getIndustry());
        company.setIntroduce(userAndCompany.getIntroduce());
        company.setLogo("../../static/img/"+userAndCompany.getLogo());
        company.setName(userAndCompany.getName());
        company.setVisitCount(0);
        company.setStatus("待审核");
        company.setHot("非热点");
        companyMapper.insert(company);
        //将招聘者与公司关联
        companyMapper.addUserAndCompany(userAndCompany.getUid(),company.getCompanyId());
    }

    //根据招聘者id查询公司信息
    @Override
    public Company queryCompanyByUid(Integer uid) {
        Company company = companyMapper.queryCompanyByUid(uid);
        return company;
    }

    //修改公司信息
    @Override
    public void updateCompany(UserAndCompany userAndCompany) {
        Company company = new Company();
        company.setCompanyId(userAndCompany.getCompanyId());
        company.setAddress(userAndCompany.getAddress());
        company.setIndustry(userAndCompany.getIndustry());
        company.setIntroduce(userAndCompany.getIntroduce());
        company.setName(userAndCompany.getName());
        companyMapper.updateById(company);
    }

    //根据公司id查询所有招聘人员
    @Override
    public List<User> queryStaff(Integer compId) {
        List<User> users = companyMapper.queryStaff(compId);
        return users;
    }

    //根据公司id和用户id移除招聘者
    @Override
    public void deleteStaff(Integer uid, Integer compId) {
        companyMapper.deleteStaff(uid,compId);
    }

    //根据手机号添加招聘者
    @Override
    public boolean addStaff(String tel,Integer compId) {
        //查询该用户是否注册
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("tel",tel);
        User userDB = userMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(userDB)) {
            //没有注册
            return false;
        }else {
            //存在该用户
            userMapper.updateRole(2,userDB.getId());//将其设置为招聘者角色
            companyMapper.addUserAndCompany(userDB.getId(),compId);//并将其与公司关联
            companyMapper.updateUserCount(compId);//将公司的招聘人数+1
            return true;
        }

    }
}
